﻿using System.Windows;
using System.Windows.Shapes;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;




namespace MSAP.Framework.Controls
{
    public class CustomWindow : Window
    {
        public CustomWindow()
            : base()
        {
            //base.WindowStyle = WindowStyle.None;
            //base.AllowsTransparency = true;

            DefaultStyleKey = typeof(CustomWindow);
            this.Loaded += new RoutedEventHandler(CustomWindow_Loaded);
            
        }

        private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
        {

            
            //Border windowFrame = new Border();
            //windowFrame.CornerRadius = new CornerRadius(10);
            //windowFrame.Background = Color.
            //base.Width = 200;
            //base.Height = 300;
            //base.Content = new Button() { Content = "test" };

            //windowFrame.Child = new Button() { Content = "test" };
            

    //        CornerRadius="0,20,30,40"

    //         <Border Width="Auto" Height="Auto" Name="windowFrame"
    //        BorderBrush="#395984" BorderThickness="2"
    //        CornerRadius="0,20,30,40" >
    //    <Border.Background>
    //        <LinearGradientBrush ColorInterpolationMode="ScRgbLinearInterpolation">
    //            <GradientBrush.GradientStops>
    //                <GradientStopCollection>
    //                    <GradientStop Color="#E7EBF7" Offset="0.0"/>
    //                    <GradientStop Color="#CEE3FF" Offset="0.5"/>
    //                </GradientStopCollection>
    //            </GradientBrush.GradientStops>
    //        </LinearGradientBrush>
    //    </Border.Background>
    //    <Grid>
    //        <Grid.RowDefinitions>
    //            <RowDefinition Height="Auto"></RowDefinition>
    //            <RowDefinition></RowDefinition>
    //            <RowDefinition Height="Auto"></RowDefinition>
    //        </Grid.RowDefinitions>

    //        <TextBlock x:Name="Title" Text="Title Bar" Margin="1" Padding="3" FontWeight="Bold"
    //                   MouseLeftButtonDown="Title_MouseLeftButtonDown"></TextBlock>

    //        <StackPanel Grid.Row="1" Background="Lavender">
    //            <TelerikGrid:RadGridView x:Name="GridView" ScrollMode="RealTime" 
    //                         Grid.Row="1" ColumnsWidthMode="Auto" 
    //                         UseAlternateRowStyle="True" />
    //        </StackPanel>

    //        <TextBlock x:Name="Footer" Grid.Row="2" Text="Footer"  VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

    //        <Rectangle Grid.Row="0" Grid.RowSpan="3" Width="5"
    //                VerticalAlignment="Stretch" HorizontalAlignment="Right"
    //                Cursor="SizeWE" Fill="Transparent"
    //                MouseLeftButtonDown="window_ResizeBegin"
    //                MouseLeftButtonUp="window_ResizeEnd"
    //                MouseMove="window_ResizeWidth">
    //        </Rectangle>

    //        <Rectangle Grid.Row="2" Width="Auto" Height="5"
    //                VerticalAlignment="Bottom" HorizontalAlignment="Stretch"
    //                Cursor="SizeNS" Fill="Transparent"
    //                MouseLeftButtonDown="window_ResizeBegin"
    //                MouseLeftButtonUp="window_ResizeEnd"
    //                MouseMove="window_ResizeHeight">
    //        </Rectangle>

    //        <Rectangle Grid.Row="2" Width="30" Height="23"
    //                VerticalAlignment="Bottom" HorizontalAlignment="Right"
    //                Cursor="SizeNWSE" Fill="Transparent"
    //                MouseLeftButtonDown="window_ResizeBegin"
    //                MouseLeftButtonUp="window_ResizeEnd"
    //                MouseMove="window_Resize">
    //        </Rectangle>

    //        <Button x:Name="btnCloseWindow" Grid.Row="0" Width="16" Height="16" Margin="0,0,10,3"
    //                VerticalAlignment="Bottom" HorizontalAlignment="Right" Click="btnCloseWindow_Click">
    //        </Button>

    //    </Grid>
    //</Border>
        }

        #region --------------------------- Events -------------------------------
        private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        bool isWiden = false;
        private void window_ResizeBegin(object sender, MouseEventArgs e)
        {
            isWiden = true;
        }

        private void window_ResizeEnd(object sender, MouseEventArgs e)
        {
            isWiden = false;
            // Make sure capture is released.
            Rectangle rect = (Rectangle)sender;
            rect.ReleaseMouseCapture();
        }

        private void window_ResizeWidth(object sender, MouseEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;
            if (isWiden)
            {
                rect.CaptureMouse();
                double newWidth = e.GetPosition(this).X + 5;
                if (newWidth > 0) this.Width = newWidth;
            }
        }

        private void window_ResizeHeight(object sender, MouseEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;
            if (isWiden)
            {
                rect.CaptureMouse();
                double newHeight = e.GetPosition(this).Y + 5;
                if (newHeight > 0) this.Height = newHeight;
            }
        }

        private void window_Resize(object sender, MouseEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;
            if (isWiden)
            {
                rect.CaptureMouse();
                double newWidth = e.GetPosition(this).X + 5;
                if (newWidth > 0) this.Width = newWidth;
                double newHeight = e.GetPosition(this).Y + 5;
                if (newHeight > 0) this.Height = newHeight;
            }
        }

        private void btnCloseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion --------------------------- Events-------------------------------
    }
}
